﻿using System;
using System.Net;

namespace SPWarmup
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(@"******************************************************
* SPWarmUp - powered by SharePointTrench / CodeForce *
*                                                    *
******************************************************");
                
                Console.WriteLine("Warming up Sharepoint web applications / application pools..");
                Console.ResetColor();

                ITopologyService topologyService = new TopologyService();
                var webApplications = topologyService.GetContentApplications();

                Console.WriteLine("Found {0} Web Applications", webApplications.Count);
                
                foreach (var webApp in webApplications)
                {
                    try
                    {
                        Console.Write("Warming up {0}", webApp.Name);
                        webApp.WarmUp();
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("  - OK -");
                        Console.ResetColor();
                    }
                    catch (WebException ex)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Error.WriteLine("  Error warming up {0}: {1}", webApp.Name, ex.Message);
                        Console.ResetColor();
                    }
                    catch (Exception ex)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Error.WriteLine("  Error warming up {0}: {1}", webApp.Name, ex.Message);
                        Console.ResetColor();
                    }

                }
            }
            finally
            {
                Console.ResetColor();
            }

        }

    }
}
